/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.generator.controller;

import net.db.generator.JavaBaseClassWriter;

/**
 *
 * @author abosancic
 */
public class JSFBaseControllerClassWriter extends JavaBaseClassWriter {

    public JSFBaseControllerClassWriter() {
        super();
    }

    @Override
    protected void generateClassName() {
        stringClass.append("public class " + getClassName() + "<T> implements Serializable {\n\n");
        generateDAOClass();
    }

    //==========================================================================
    // setters and getters
    //==========================================================================
    @Override
    public String getClassName() {
        return "BaseController";
    }

    protected void generateDAOClass() {

        String daoClass = "    protected T selected;\n"
                + "    protected DataModel items = null;\n"
                + "\n"
                + "    protected PaginationManager pagination;\n"
                + "    protected int selectedItemIndex;\n"
                + "\n"
                + "    public BaseController() {\n"
                + "    }\n"
                + "\n"
                + "    protected DAOBase getFacade() {\n"
                + "        return null;\n"
                + "    }\n"
                + "    \n"
                + "    protected Class<T> getClassType(){\n"
                + "        return null;\n"
                + "    }\n"
                + "    \n"
                + "    public T getSelected() {\n"
                + "        if (selected == null) {\n"
                + "            try {\n"
                + "                selected = (T) getClassType().newInstance();\n"
                + "            } catch (InstantiationException ex) {\n"
                + "                Logger.getLogger(BaseController.class.getName()).log(Level.SEVERE, null, ex);\n"
                + "            } catch (IllegalAccessException ex) {\n"
                + "                Logger.getLogger(BaseController.class.getName()).log(Level.SEVERE, null, ex);\n"
                + "            }\n"
                + "            selectedItemIndex = -1;\n"
                + "        }\n"
                + "        return selected;\n"
                + "    }\n"
                + "    \n"
                + "    public PaginationManager getPagination() {\n"
                + "        if (pagination == null) {\n"
                + "            pagination = new PaginationManager(10) {\n"
                + "                @Override\n"
                + "                public int getItemsCount() {\n"
                + "                    return getFacade().countRows();\n"
                + "                }\n"
                + "\n"
                + "                @Override\n"
                + "                public DataModel createPageDataModel() {\n"
                + "                    return new ListDataModel(getFacade().findRange(new int[]{getPageFirstItem(), getPageFirstItem() + getPageSize()}));\n"
                + "                }\n"
                + "            };\n"
                + "        }\n"
                + "        return pagination;\n"
                + "    }\n"
                + "\n"
                + "    public String prepareList() {\n"
                + "        items = null;\n"
                + "        return \"List\";\n"
                + "    }\n"
                + "\n"
                + "    public String prepareView() {\n"
                + "        selected = (T) getItems().getRowData();\n"
                + "        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();\n"
                + "        return \"View\";\n"
                + "    }\n"
                + "\n"
                + "    public String prepareCreate() {\n"
                + "        try {\n"
                + "            selected = getClassType().newInstance();\n"
                + "        } catch (InstantiationException ex) {\n"
                + "            Logger.getLogger(BaseController.class.getName()).log(Level.SEVERE, null, ex);\n"
                + "        } catch (IllegalAccessException ex) {\n"
                + "            Logger.getLogger(BaseController.class.getName()).log(Level.SEVERE, null, ex);\n"
                + "        }\n"
                + "        selectedItemIndex = -1;\n"
                + "        return \"Create\";\n"
                + "    }\n"
                + "\n"
                + "    public String create() {\n"
                + "       return null;\n"
                + "    }\n"
                + "\n"
                + "    public String prepareEdit() {\n"
                + "        selected = (T) getItems().getRowData();\n"
                + "        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();\n"
                + "        return \"Edit\";\n"
                + "    }\n"
                + "\n"
                + "    public String destroy() {\n"
                + "        selected = (T) getItems().getRowData();\n"
                + "        selectedItemIndex = pagination.getPageFirstItem() + getItems().getRowIndex();\n"
                + "        performDestroy();\n"
                + "        recreatePagination();\n"
                + "        return prepareList();\n"
                + "    }\n"
                + "\n"
                + "    public String destroyAndView() {\n"
                + "        performDestroy();\n"
                + "        items = null;\n"
                + "        updateCurrentItem();\n"
                + "        if (selectedItemIndex >= 0) {\n"
                + "            return \"View\";\n"
                + "        } else {\n"
                + "            return prepareList();\n"
                + "        }\n"
                + "    }\n"
                + "\n"
                + "    protected void performDestroy() {\n"
                + "       \n"
                + "    }\n"
                + "\n"
                + "    protected void updateCurrentItem() {\n"
                + "        int count = getFacade().countRows();\n"
                + "        if (selectedItemIndex >= count) {\n"
                + "            selectedItemIndex = count - 1;\n"
                + "            if (pagination.getPageFirstItem() >= count) {\n"
                + "                pagination.previousPage();\n"
                + "            }\n"
                + "        }\n"
                + "        if (selectedItemIndex >= 0) {\n"
                + "            selected = (T) getFacade().findRange(new int[]{selectedItemIndex, selectedItemIndex + 1}).get(0);\n"
                + "        }\n"
                + "    }\n"
                + "\n"
                + "    public DataModel getItems() {\n"
                + "        if (items == null) {\n"
                + "            items = getPagination().createPageDataModel();\n"
                + "        }\n"
                + "        return items;\n"
                + "    }\n"
                + "\n"
                + "    protected void recreatePagination() {\n"
                + "        pagination = null;\n"
                + "    }\n"
                + "\n"
                + "    public String next() {\n"
                + "        getPagination().nextPage();\n"
                + "        return prepareList();\n"
                + "    }\n"
                + "\n"
                + "    public String previous() {\n"
                + "        getPagination().previousPage();\n"
                + "        return prepareList();\n"
                + "    }\n"
                + "\n"
                + "    public void nextAjax(AjaxBehaviorEvent event) {\n"
                + "        next();\n"
                + "    }\n"
                + "    \n"
                + "    public void previousAjax(AjaxBehaviorEvent event) {\n"
                + "        previous();\n"
                + "    }"
                + "\n"
                + "    public SelectItem[] getItemsAvailableSelectMany() {\n"
                + "        return JsfUtil.getSelectItems(getFacade().findAll(), false);\n"
                + "    }\n"
                + "\n"
                + "    public SelectItem[] getItemsAvailableSelectOne() {\n"
                + "        return JsfUtil.getSelectItems(getFacade().findAll(), true);\n"
                + "    }\n"
                + "\n"
                + "    public abstract static class BaseControllerConverter implements Converter {\n"
                + "\n"
                + "        protected java.lang.Integer getKey(String value) {\n"
                + "            java.lang.Integer key;\n"
                + "            key = Integer.valueOf(value);\n"
                + "            return key;\n"
                + "        }\n"
                + "\n"
                + "        protected String getStringKey(java.lang.Integer value) {\n"
                + "            StringBuffer sb = new StringBuffer();\n"
                + "            sb.append(value);\n"
                + "            return sb.toString();\n"
                + "        }\n"
                + "\n"
                + "    }\n";
        stringClass.append(daoClass);
    }

    @Override
    protected void generateFields() {
    }

    @Override
    protected void generateToString() {
    }

    @Override
    protected void generateHashCode() {
    }

    @Override
    protected void generateEquals() {
    }

    @Override
    protected void generateConstructor() {
    }

    @Override
    protected void generateGettersAndSetters() {
    }
}
